<!-- 药品管理类 -->
<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <!-- <el-form-item prop="taskState">
                <el-input v-model="filters.taskState" clearable placeholder="任务状态"></el-input>
              </el-form-item>
              <el-form-item prop="taskType">
                <el-input v-model="filters.taskType" clearable placeholder="任务类型"></el-input>
              </el-form-item> -->
            <el-form-item prop="batchCode">
              <el-input v-model="filters.batchCode" clearable placeholder="批次码"></el-input>
            </el-form-item>
            <el-form-item prop="taskState">
              <el-select v-model="filters.taskState" clearable placeholder="任务状态">
                <el-option v-for="item in taskStateList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="taskType">
              <el-select v-model="filters.taskType" clearable placeholder="任务类型">
                <el-option v-for="item in taskTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="institutionId">
              <el-select v-model="filters.institutionId" clearable placeholder="医检机构" filterable>
                <el-option v-for="item in institutionList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="hospitalId">
              <el-select v-model="filters.hospitalId" clearable placeholder="医院" filterable>
                <el-option v-for="item in hospitalList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="syUserId">
              <el-select v-model="filters.syUserId" clearable placeholder="收样员" filterable>
                <el-option v-for="item in collectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <!-- <el-button
              type="primary"
              plain
              icon="fa fa-plus"
              @click="handleClickAdd()"
              size="mini"
            >
              新增</el-button
            > -->
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column type="index" align="conter" label="序号"></el-table-column>
        <el-table-column prop="institutionName" label="医检" align="center"></el-table-column>
        <el-table-column prop="hospitalSplicingName" label="医院-科室-姓名" width="300" align="center"></el-table-column>
        <el-table-column prop="syUserName" label="收样员" align="center"></el-table-column>
        <el-table-column prop="batchCode" label="批次码" align="center"></el-table-column>
        <el-table-column label="任务状态" align="center">
          <template slot-scope="scope">
            {{ formatTaskState(scope.row.taskState) }}
          </template>
        </el-table-column>
        <el-table-column label="任务类型" align="center">
          <template slot-scope="scope">
            {{ formatTaskType(scope.row.taskType) }}
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center"> </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionRecord" @click="handleClickDetail(scope.row)" size="mini">录单</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionState" @click.native="handleClickCancel(scope.row, 15)">取消</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleIsDelete(scope.row, -1)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import TaskApi from '@/api/task/task';
import InstitutionApi from '@/api/institution/institution';
import CollectApi from '@/api/collect/collect';
import HospitalApi from '@/api/institution/hospital';
import { getStorage } from '@/utils/auth';
import * as stn from '@/constants/storeName';

export default {
  name: 'TaskManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      hospitalList: [],
      institutionList: [],
      collectList: [],
      // 状态：(-1.删除 0.禁用 1.启用
      taskStateList: [
        { label: '待录单', value: 1 },
        { label: '录单中', value: 5 },
        { label: '已录单', value: 10 },
        { label: '已取消', value: 15 },
      ],
      taskTypeList: [
        { label: '常规', value: 1 },
        { label: '核酸', value: 2 },
      ],
      filters: {
        // 搜索框内容
        taskState: '',
        taskType: '',
        institutionId: '',
        hospitalId: '',
        syUserId: '',
        batchCode: '',
        pageSize: '',
        pageNo: '',
      },

      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionRecord: 'SYSTEM_MANAGER_TASK_INFO_LIST_RECORD', // 查看
      permissionDelete: 'SYSTEM_MANAGER_TASK_INFO_LIST_DELETE', // 删除
      permissionState: 'SYSTEM_MANAGER_TASK_INFO_LIST_STATE', // 药品列表状态 启用/禁用
      excelData: {},
      allList: [],
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    // if(this.$route.meta.keepAlive){
    this.getTableHeight();
    this.listData();
    // }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      this.userInfo = getStorage(stn.USER_INFO);
      // 把个人信息暂时存储在网页中
      if (this.userInfo) {
        this.userRole = this.userInfo.userRole;
        // 用户状态，以控制权限
      }
      _this.listInstitution();
      _this.listCollect();
      _this.listHospital();
      _this.getTableHeight();
      _this.listData();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getTableHeight() {
      // 设置表格高度
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    formatTaskState(value) {
      if (value == 1) {
        return '待录单';
      } else if (value == 5) {
        return '录单中';
      } else if (value == 10) {
        return '已录单';
      } else if (value == 15) {
        return '已取消';
      }
    },
    formatTaskType(value) {
      if (value == 1) {
        return '常规';
      } else if (value == 2) {
        return '核酸';
      }
    },
    listCollect() {
      let _this = this;
      _this.loadingIns = true;
      let cond = { roleId: 4 }; // 暂不传入任何参数
      // collectNameList--/admin/listAdmin  获取收样员名字列表
      CollectApi.listCollectUserName(cond).then((res) => {
        setTimeout(() => {
          _this.loadingIns = false;
          _this.collectList = res.data.map((item) => {
            return {
              value: item.userId,
              label: item.userName,
            };
          });
        }, 200);
      });
    },

    listHospital() {
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        hospitalType: 1,
      };
      HospitalApi.getHospitalNameList(cond).then((res) => {
        setTimeout(() => {
          _this.loadingIns = false;
          _this.hospitalList = res.data.map((item) => {
            return {
              value: item.hospitalId,
              label: item.hospitalName,
            };
          });
        }, 200);
      });
    },
    listInstitution(query) {
      let _this = this;
      let institutionName = !query || typeof query !== 'string' ? '' : query;
      _this.loadingIns = true;
      InstitutionApi.selectInsByUser(institutionName)
        .then((res) => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.institutionList = res.data.map((item) => {
              return {
                value: item.institutionId,
                label: item.institutionName,
              };
            });
          }, 200);
        })
        .catch((error) => {
          _this.institutionList = [];
          console.error(error);
        });
    },
    resetForm() {
      // 重置表单
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },

    handleClickDetail(scope) {
      // 录单
      let _this = this;
      _this.$router.push({
        path: '/task/taskDetail',
        query: {
          id: scope.reportTaskId,
        },
      });
    },

    // 表单状态管理
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    // 分页插件
    handleClickCancel(row, state) {
      let _this = this;
      this.$confirm('是否确认取消', message.TITLE_PROMPT).then(() => {
        TaskApi.cancelTask({
          id: row.reportTaskId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listData();
        });
      });
    },
    handleIsDelete(row, state) {
      let _this = this;
      _this.$confirm('是否确认删除', message.TITLE_PROMPT).then(() => {
        TaskApi.deleteTask({
          id: row.reportTaskId,
          state: state,
        }).then((res) => {
          _this.$message({
            type: 'success',
            message: '操作成功',
          });
          _this.listData();
        });
      });
    },
    // 编辑状态功能
    onSearch() {
      // 搜索按钮
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 查询列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        taskState: _this.filters.taskState,
        taskType: _this.filters.taskType,
        institutionId: _this.filters.institutionId,
        hospitalId: _this.filters.hospitalId,
        syUserId: _this.filters.syUserId,
        batchCode: _this.filters.batchCode,
      };
      TaskApi.listTask(cond).then((res) => {
        // 定义分页内容
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
    // handleClickAdd() {
    // this.$router.push({ path: '/task/taskAdd'});
    // }
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
<style lang="scss"></style>
